﻿
using System.Collections.Generic;
using NHibernate;


namespace ProyectoSelva.Daos
{
    public class BaseDAO<T>
    {
     
        public static ISessionFactory CreateSessionFactory()
        {

            return SessionFactoryHelper.GetSessionFactory();
        }

        
        public IList<T> ListarTodos()
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    return session
                           .CreateCriteria(typeof(T))
                           .List<T>();
                }
            }
        }
        
        
        public T Insert(T obj)
        {

            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Save(obj);

                    transaction.Commit();
                }
                return obj;
            }

        }

        public T Update(T obj)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Update(obj);

                    transaction.Commit();
                }
                return obj;
            }
        }



        public void Delete(T obj)
        {
            using (var session = CreateSessionFactory().OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session
                    .Delete(obj);

                    transaction.Commit();
                }
            }
          
        }

    }


}
